import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: {
    per_page: 0,
    total: 0,
  },
  Fields: [],
  itemDetail: {},
  mapLists: [],
  historyDataLists: {
    // data: []
  },
  historyFields: [],
};
const actions = {
  // 列表展示
  getLists({ commit }, page) {
    if (!page) {
      // 防止数据保留
      commit('setLists', []);
      return null;
    }
    if (page.equipment_id == undefined) {
      showError('请选择设备及设备数据');
      return null;
    }
    if (page.register_id == undefined) {
      showError('请选择设备数据');
      return null;
    }
    request.get(Server.action.data_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          const lineData = response.data.result.spot;
          listData.chartData = {
            value: [],
            date: [],
          };
          lineData.forEach((item, i) => {
            listData.chartData.value.push(Number(item.data_value));
            listData.chartData.date.push(item.created_at);
          });
          // console.log(listData.chartData);
          commit('setLists', listData);
          const { fields } = response.data.result;
          fields.forEach((item, i) => {
            fields[i].dataIndex = fields[i].key;
            fields[i].align = 'center';
            if (i < fields.length - 1) {
              fields[i].width = item.width || 110;
            }
          });
          commit('setFields', fields);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 详情
  dataDetail({ commit }) {
    request.get(Server.action.YieldReport_detail)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setDetail', response.data.result);
        // showSuccess(response.data.result)
        } else {
          showError(response.data.message);
        }
      });
  },

  // 历史报表
  dataHistoryList({ commit }, page) {
    if (!page) {
      commit('setHistoryLists', []);
      return null;
    }
    request.get(Server.action.data_historyForm, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          const listData = response.data.result.list;
          // listData.data = listData.data.data
          // delete listData.data.data
          // console.log(listData);
          commit('setHistoryLists', listData);
          let { fields } = response.data.result;
          const obj = {
            align: 'center',
            dataIndex: 'datetime',
            key: 'datetime',
            title: '更新日期',
            width: 160,
            fixed: 'left',
          };
          fields = [obj, ...fields];
          listData.itemNumber = (fields.length - 1) * 100 + 150;
          listData.fieldsName = [];
          fields.forEach((item, i) => {
            if (i > 0) {
              listData.fieldsName.push(item.key);
            }
            item.dataIndex = item.key;
            item.scopedSlots = { customRender: item.key };
            item.align = 'center';
            // if(i < fields.length-1) {
            item.width = item.width || 100;
            // }
          });
          commit('setHistoryFields', fields);
        } else {
          showError(response.data.message);
        }
      });
  },
  dataHistoryEexport({ commit }, data) {
    data.export_page = 1;
    console.log(data);
    return request.get(Server.action.data_historyForm, { timeout: 30000, params: data });
  },

  // 下载导出数据
  exportData({ commit }, data) {
    const params = JSON.parse(JSON.stringify(data));
    delete params.page;
    console.log(params);
    return request.get(Server.action.exportHistoryData, { params, responseType: 'blob' });
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
  setDetail(state, data) {
    state.itemDetail = data;
  },
  setMapLists(state, lists) {
    state.mapLists = lists;
  },
  setHistoryLists(state, lists) {
    state.historyDataLists = lists;
  },
  setHistoryFields(state, lists) {
    state.historyFields = lists;
  },
};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
